import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
import hitcxeduHttp from 'hitcxedu-http';
import utils from './utils';
import { Message, Notification } from 'element-ui'
import hitcxeduUi from 'hitcxedu-ui'
<<<<<<< HEAD
import api from '@/api'
=======
>>>>>>> 85d0debcca44c4a451c9e968272089f1bfdcaf45
import './permission';
import './components';
import './icons';

Vue.config.productionTip = false
Vue.use(utils)
let instance = null;

Vue.use(hitcxeduUi)
<<<<<<< HEAD
Vue.use(api)
=======
>>>>>>> 85d0debcca44c4a451c9e968272089f1bfdcaf45
Vue.prototype.$log.isLog = process.env.ENV !== 'production'

hitcxeduHttp.setOptions({
  baseURL: process.env.VUE_APP_BASE_API,
  // 请求正确的code
  successCode: 0,
  // 请求发出前
  request(config) {
    config.headers['Authorization'] = store.getters.token
    return config;
  },
  // 请求发送失败
  requestError(error) {
    console.log(error);
  },
  // 成功返回
  responseSuccess(response) {
    const msg = response.config._msg;
    if (msg) {
      Message.success({
        showClose: true,
        message: response.data.msg || msg,
        type: 'success'
      })
    }
  },
  // 常见的公共错误处理
  commonErrorHandling(response) {
    switch (response.data.code) {
      case 101:
        location.href = `${process.env.VUE_APP_LOGIN_PATH}?redirect=${escape(location.href)}`
        break;
      default:
        Notification.error({ title: '错误', message: response.data.msg || '服务器连接错误！' })
    }
  },
  // 返回错误
  responseError(error) {
    console.log(error)
    Notification.error({ title: '错误', message: '服务器连接错误！' })
  }
});

function render(props = {}) {
  const { container } = props;
  instance = new Vue({
    store,
    router,
    render: h => h(App)
  }).$mount(container ? container.querySelector('#app') : '#app')
}

if (!window.__POWERED_BY_QIANKUN__) {
  render();
}

function storeTest(props) {
  props.onGlobalStateChange &&
    props.onGlobalStateChange(
      (value, prev) => console.log(`[onGlobalStateChange - ${props.name}]:`, value, prev),
      true,
    );
  props.setGlobalState &&
    props.setGlobalState({
      ignore: props.name,
      user: {
        name: props.name,
      },
    });
}

export async function bootstrap() {
  console.log('[vue] vue app bootstraped');
}

export async function mount(props) {
  storeTest(props);
  render(props);
}

export async function unmount() {
  instance.$destroy();
  instance = null;
}
